package jmotioncapture.testing.framework;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class SettingPanel {

	private JTextField number_of_frames_decide_static_pointTextField;

	private JTextField length_toleranceTextField;

	private JTextField angle_toleranceTextField;

	/**
	 * GUI Variable
	 */
	private JTextField static_point_toleranceTextField;

	/**
	 * GUI Variable
	 */
	protected JFrame frame;

	/**
	 * Create the application
	 */
	public SettingPanel() {

		initialize();

		this.static_point_toleranceTextField
				.setText(String
						.valueOf(jmotioncapture.calculation.FrameCalculation.STATIC_POINT_TOLERANCE));

		this.angle_toleranceTextField.setText(String
				.valueOf(jmotioncapture.geometry2D.Rectangle.ANGLE_TOLERANCE));

		this.length_toleranceTextField.setText(String
				.valueOf(jmotioncapture.geometry2D.Square.LENGTH_TOLERANCE));

		this.number_of_frames_decide_static_pointTextField
				.setText(String
						.valueOf(jmotioncapture.calculation.FrameCalculation.NUMBER_OF_FRAMES_DECIDE_STATIC_POINT));
	}

	/**
	 * Initialize the contents of the frame
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		frame.setTitle("Setting");
		frame.setBounds(100, 100, 422, 263);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		final JButton confirmButton = new JButton();
		confirmButton.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent arg0) {

				try {
					jmotioncapture.calculation.FrameCalculation.STATIC_POINT_TOLERANCE = Double
							.parseDouble(static_point_toleranceTextField
									.getText());

					jmotioncapture.calculation.FrameCalculation.NUMBER_OF_FRAMES_DECIDE_STATIC_POINT = Integer
							.parseInt(number_of_frames_decide_static_pointTextField
									.getText());

					jmotioncapture.geometry2D.Rectangle.ANGLE_TOLERANCE = Double
							.parseDouble(angle_toleranceTextField.getText());

					jmotioncapture.geometry2D.Square.LENGTH_TOLERANCE = Double
							.parseDouble(length_toleranceTextField.getText());

				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Error: Static Count",
							"Error: Static Count ", JOptionPane.ERROR_MESSAGE);
				}

				// Closing
				frame.setVisible(false);
				frame.dispose();
			}
		});
		confirmButton.setText("Confirm");
		confirmButton.setBounds(215, 204, 93, 23);
		frame.getContentPane().add(confirmButton);

		final JButton cancelButton = new JButton();
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				// Closing
				frame.setVisible(false);
				frame.dispose();
			}
		});
		cancelButton.setText("Cancel");
		cancelButton.setBounds(314, 204, 93, 23);
		frame.getContentPane().add(cancelButton);

		final JLabel static_point_toleranceLabel = new JLabel();
		static_point_toleranceLabel.setText("STATIC_POINT_TOLERANCE:");
		static_point_toleranceLabel.setBounds(20, 33, 302, 15);
		frame.getContentPane().add(static_point_toleranceLabel);

		static_point_toleranceTextField = new JTextField();
		static_point_toleranceTextField.setBounds(322, 30, 85, 20);
		frame.getContentPane().add(static_point_toleranceTextField);

		final JLabel angle_toleranceLabel = new JLabel();
		angle_toleranceLabel.setText("ANGLE_TOLERANCE:");
		angle_toleranceLabel.setBounds(20, 108, 285, 15);
		frame.getContentPane().add(angle_toleranceLabel);

		angle_toleranceTextField = new JTextField();
		angle_toleranceTextField.setBounds(322, 105, 85, 20);
		frame.getContentPane().add(angle_toleranceTextField);

		final JLabel length_toleranceLabel = new JLabel();
		length_toleranceLabel.setText("LENGTH_TOLERANCE:");
		length_toleranceLabel.setBounds(20, 169, 285, 15);
		frame.getContentPane().add(length_toleranceLabel);

		length_toleranceTextField = new JTextField();
		length_toleranceTextField.setBounds(322, 166, 85, 20);
		frame.getContentPane().add(length_toleranceTextField);

		final JLabel number_of_frames_decide_static_pointLabel = new JLabel();
		number_of_frames_decide_static_pointLabel
				.setText("NUMBER_OF_FRAMES_DECIDE_STATIC_POINT:");
		number_of_frames_decide_static_pointLabel.setBounds(20, 54, 302, 15);
		frame.getContentPane().add(number_of_frames_decide_static_pointLabel);

		final JLabel jmotioncapturecalculationframecalculationLabel = new JLabel();
		jmotioncapturecalculationframecalculationLabel.setFont(new Font("",
				Font.BOLD, 12));
		jmotioncapturecalculationframecalculationLabel
				.setText("jmotioncapture.calculation.FrameCalculation:");
		jmotioncapturecalculationframecalculationLabel
				.setBounds(10, 7, 260, 20);
		frame.getContentPane().add(
				jmotioncapturecalculationframecalculationLabel);

		final JLabel jmotioncapturegeometry2dRectangleLabel = new JLabel();
		jmotioncapturegeometry2dRectangleLabel.setFont(new Font("", Font.BOLD,
				12));
		jmotioncapturegeometry2dRectangleLabel
				.setText("jmotioncapture.geometry2D.Rectangle:");
		jmotioncapturegeometry2dRectangleLabel.setBounds(10, 82, 225, 20);
		frame.getContentPane().add(jmotioncapturegeometry2dRectangleLabel);

		final JLabel jmotioncapturegeometry2dsquareLabel = new JLabel();
		jmotioncapturegeometry2dsquareLabel
				.setFont(new Font("", Font.BOLD, 12));
		jmotioncapturegeometry2dsquareLabel
				.setText("jmotioncapture.geometry2D.Square:");
		jmotioncapturegeometry2dsquareLabel.setBounds(10, 143, 205, 20);
		frame.getContentPane().add(jmotioncapturegeometry2dsquareLabel);

		number_of_frames_decide_static_pointTextField = new JTextField();
		number_of_frames_decide_static_pointTextField
				.setBounds(322, 56, 84, 20);
		frame.getContentPane().add(
				number_of_frames_decide_static_pointTextField);
	}

}
